﻿using System;
using System.Collections.Generic;
using System.IO;
using System.ServiceModel;
using System.Web;
using SecurityClient;

namespace SecurityModule.Workflows
{
    class WFGetHomeResetPassword : SimpleWorkflow
    {
        public WFGetHomeResetPassword()
        {
            Verb = "GET";
            Path = "/Home/ResetPassword";
        }

        public override void PreProcessImplementation(HttpContext context)
        {
            var tokenId = context.Request.QueryString["ResetToken"];

            if (!string.IsNullOrEmpty(tokenId))
            {
                try
                {
                    //Anon is attempting to confirm their identity
                    var security = new SecurityServiceProxy();
                    var email = security.GetTokenFieldValue(tokenId, "EmailAddress") as string;
                    if (string.IsNullOrEmpty(email))
                    {
                        AddToForm(context.Request,
                                  new Dictionary<string, string>
                                          {
                                              {"EXTERRMSG", "Invalid reset token"}
                                          });
                    }
                    else // token is verified
                    {
                        AddToForm(context.Request,
                                  new Dictionary<string, string>
                                          {
                                              {"Email", email}
                                          });
                    }
                }
                catch (Exception exception)
                {
                    if (exception is PipeException || exception is EndpointNotFoundException)
                    {
                        AddToForm(context.Request, new Dictionary<string, string>
                            {
                                {"EXTERRMSG", SecurityServiceDownMsg}
                            });
                    }
                    else
                    {
                        AddToForm(context.Request, new Dictionary<string, string>
                            {
                                {"EXTERRMSG", exception.Message}
                            });
                    }
                }
            }
            else
            {
                AddToForm(context.Request,
                          new Dictionary<string, string>
                                      {
                                          {"EXTERRMSG", "Missing reset token"}
                                      });
            }
            //inject YubikeyOTP input using replace
            context.Response.Filter = new ReplaceFilter(context.Response.Filter,
                context.Response.ContentEncoding,
                content =>
                {
                    content = content.Replace("{{YubikeyOTP}}",
                                                @"<label class='control-label' for='YubikeyOTP'>Yubikey</label><input name='YubikeyOTP' type='text' class='form-control'>");
                    // the user should trust the sensitve information they provide in this page will never be visible to the app directly, all JS code (except for trusted sources) should be cleaned to prevent potential AJAX calls
                    //content = content.RemoveEventsAndScripts();
                    return content;
                }
            );
        }

        public override void PostProcessImplementation(HttpContext context)
        {
            // No post-processing
        }
    }
}
